package com.server.dashboard;

import com.dao.dashboard.DashboardsMapper;
import com.dao.user.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.model.bo.DashOrder;
import com.model.po.Dashboards;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.pojo.TestPage;
import com.model.vo.configVo.ChangeOrderInfo;
import com.model.vo.configVo.DashboardsInfo;
import com.model.vo.dataVo.DashboardOrderInfo;
import com.server.chart.ChartsUtilService;
import com.util.GetTokenData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DashboardsService {
    @Autowired
    DashboardsMapper dashboardsMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    GetTokenData getTokenData;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ChartsUtilService chartsUtilService;

    /*
    保存看板
     */
    public RepEntity setDashboards(String token, DashboardsInfo dashboardsInfo) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        String name = stringMap.get("name");
        Dashboards dashboards = new Dashboards();
        BeanUtils.copyProperties(dashboardsInfo, dashboards);
        dashboards.setCreateBy(name);
        dashboards.setCreateId(userId);
        dashboardsMapper.setDashboards(dashboards);
        int id = dashboards.getId();
        return new RepEntity(RepCode.success, id);
    }

    /*
    更新看板
     */
    public RepEntity updateDashboards(String token, DashboardsInfo dashboardsInfo) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        String name = stringMap.get("name");
        int createId = dashboardsMapper.getCreateIdById(dashboardsInfo.getId());
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }
        Dashboards dashboards = new Dashboards();
        BeanUtils.copyProperties(dashboardsInfo, dashboards);
        dashboards.setCreateBy(name);
        dashboards.setCreateId(userId);

        dashboardsMapper.updateDashboards(dashboards);
        return new RepEntity(RepCode.success);
    }

    /*
    删除看板
     */
    public RepEntity delDashboards(String token, List<Integer> idList) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        Iterator isList = idList.iterator();
        while (isList.hasNext()) {
            int id = (int) isList.next();
            int createId = dashboardsMapper.getCreateIdById(id);
            if (userId != createId) {
                return new RepEntity(RepCode.NoAuthority);
            }
        }
        dashboardsMapper.delDashboards(idList);
        return new RepEntity(RepCode.success);
    }

    /*
    查看看板
     */
    public RepEntity getListDashboards(String token, TestPage testPage) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int id = Integer.parseInt(stringMap.get("id"));
        List<Dashboards> getListDashboards = dashboardsMapper.getListDashboards(id, testPage.enablePaging());
        PageInfo<Dashboards> pageInfo = new PageInfo<>(getListDashboards);
        return new RepEntity(RepCode.success, pageInfo);
    }

    /*
    查看单个看板
     */
    public RepEntity getDashboards(String token, int id) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        Dashboards dashboards = dashboardsMapper.getDashboards(userId, id);
        if (dashboards == null){
            return new RepEntity(RepCode.DashboardNonExistent);
        }
        return new RepEntity(RepCode.success, dashboards);
    }

    /*
    转交看板
     */
    public RepEntity changeDashOrder(String token, ChangeOrderInfo changeOrderInfo) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        int createId = dashboardsMapper.getCreateIdById(changeOrderInfo.getId());
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }
        String name = userMapper.getName(changeOrderInfo.getUserId());
        dashboardsMapper.changeDashOrder(name, changeOrderInfo.getUserId(), changeOrderInfo.getId());
        return new RepEntity(RepCode.success);
    }

    /*
    查看看板分发对象
     */
    public RepEntity getDashOrder(String token, int dashId) {
        Map<String, String> stringMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        int createId = dashboardsMapper.getCreateIdById(dashId);
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }

        List<DashOrder> userNames = new ArrayList<>();
        List<DashOrder> groupNames = new ArrayList<>();
        List<String> type = dashboardsMapper.getType(dashId);
        Iterator isType = type.iterator();
        while (isType.hasNext()){
            String ty = String.valueOf(isType.next());
            if ("1".equals(ty)){
                userNames = dashboardsMapper.getOrderName(dashId);
            }else {
                groupNames = dashboardsMapper.getOrderGroupName(dashId);
            }
        }
        DashboardOrderInfo dashboardOrderInfo = new DashboardOrderInfo();
        dashboardOrderInfo.setUserNames(userNames);
        dashboardOrderInfo.setGroupNames(groupNames);
        return new RepEntity(RepCode.success, dashboardOrderInfo);
    }
}
